package cn.edu.tju.wallet.controller;

import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.ResultCodeEnum;
import cn.edu.tju.wallet.domain.Transaction.TransactionType;
import cn.edu.tju.wallet.dto.TransactionDTO;
import cn.edu.tju.wallet.service.TransactionQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/transaction")
public class TransactionQueryController {

    @Autowired
    private TransactionQueryService transactionQueryService;

    /**
     * 查询交易流水（完全遵循指南代码，适配现成HttpResult）
     */
    @GetMapping("/list")
    public HttpResult<Page<TransactionDTO>> getTransactions(
            @RequestParam Long walletId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Page<TransactionDTO> transactions = transactionQueryService
                    .getTransactions(walletId, page, size);
            return HttpResult.success(transactions);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }

    /**
     * 按类型查询（完全遵循指南代码，适配现成HttpResult）
     */
    @GetMapping("/list/by-type")
    public HttpResult<Page<TransactionDTO>> getTransactionsByType(
            @RequestParam Long walletId,
            @RequestParam TransactionType type,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Page<TransactionDTO> transactions = transactionQueryService
                    .getTransactionsByType(walletId, type, page, size);
            return HttpResult.success(transactions);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }

    /**
     * 按时间范围查询（完全遵循指南代码，适配现成HttpResult）
     */
    @GetMapping("/list/by-time")
    public HttpResult<Page<TransactionDTO>> getTransactionsByTimeRange(
            @RequestParam Long walletId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Page<TransactionDTO> transactions = transactionQueryService
                    .getTransactionsByTimeRange(walletId, startTime, endTime, page, size);
            return HttpResult.success(transactions);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }
}